package com.yuguoxy.mytomcat;

import java.io.IOException;
import java.io.InputStream;

/**
 * 读取客户端请求包内容，将流转为字符串并解析请求方法和url
 */
public class YGHttpRequest {
    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    private String method; //请求方法
    private String url;  //请求url

    public YGHttpRequest(InputStream inputStream) throws IOException {
        String content = "";
        byte[] inputStreamBytes = new byte[1024];
        int len = 0;
        if ((len = inputStream.read(inputStreamBytes)) > 0) {
            content = new String(inputStreamBytes, 0, len);
        }

        //解析请求方法和url
//       POST  /home/user/login.html  HTTP/1.1
//    Host: 114.215.86.90
//    Cache-Control: no-cache
//    Postman-Token: bd243d6b-da03-902f-0a2c-8e9377f6f6ed
//    Content-Type: application/x-www-form-urlencoded
        System.out.println(content);
        String[] requestStr = content.split("\n");
        String requestLine = requestStr[0];
        String[] arrays = requestLine.split("\\s");
        String method = arrays[0];
        String url = arrays[1];
        this.method = method;
        this.url = url;

//        inputStream.close();
    }


}
